// pages/choose_city/choose_city.js
const app = getApp();
const request = require('../../utils/request.js');
const url = require('../../utils/url.js');
const cookie = require('../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    hotcity: [],
    topBarHeight: 0, //顶部导航高度
    toView: "city0",
    right_item: ['A', 'B', 'C', 'D', 'E', 'F', 'J', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
      'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ],
    cityList: ['阿坝藏族羌族自治区', '阿克苏地区', '阿拉克'],
    list:[],
    openlist:['上海'],
    keyword: '',
    cityName:"正在加载中",
    citymodel:false,
    nochoose: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('cityId', function (res) {
      if (res) {
        cookie.getCookie('cityName', function (rev) {
           that.setData({
            cityName:rev
           })
        })
      }
    })
			that.getOpenCity();	
			that.getCity();	
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  choseCity(e){
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    this.setData({
      cityName:name
    })
    wx.setStorageSync('cityId',id);
    wx.setStorageSync('cityName',name);
    wx.showToast({
      title:"切换成功",
      icon:"none"
    });
    setTimeout(function(){
      wx.navigateBack({
        delta:1
      })
    },1500);
    
  },
  onSwitchView(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      toView:`city${index}`
    })
    console.log(this.data.toView);
  },
  getCity:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.citylist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
         that.setData({
            list:res.data.data.data
         })
       }
    })  	
  },
  getOpenCity(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.opencitylist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
         that.setData({
            openlist:res.data.data
         })
       }
    })  	
  },
  onSwitchView(e) {
    let index = e.currentTarget.dataset.index;
    var nindex = 'city'+index;
    this.setData({
      toView:nindex
    })
  },
  doSearch() {
    var that = this;
    if(!that.data.keyword){
       wx.showToast({
         title: '请输入搜索内容',
         icon:'none'
       })
       return false;
    }
    wx.navigateTo({
      url: '../choose_city/search?key=' + that.data.keyword
    })
  },
  changekeyword(e){
    var that =  this;
    that.setData({
        keyword:e.detail.value
    })
  }
})